package com.woniuxy.house.web;

import com.woniuxy.house.vo.ResponseEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 文件上传
 * @author baobo
 * @date 2022-08-29-10:46
 */
@RestController
@RequestMapping("/api/upload")
public class UploadController {
	@Value("${house.imagesavepath}")
	private String imageSavePath;
	/**
	 *实现图片上传，针对多图片或单图片
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/uploadimages")
	@ResponseBody
	public ResponseEntity uploadImage(MultipartFile file) throws IOException {

		String names=null;
			//得到上传的图片
		String contentType = file.getContentType();
			if(!contentType.equals("image/jpeg")){
				return new ResponseEntity("400","文件格式错误",null);
			}
			//生成新文件名
			String name = "imgs/"+UUID.randomUUID().toString().replace("-", "") + ".jpg";
			String newName = imageSavePath + name;
		System.out.println(newName);
			file.transferTo(new File(newName));
		return new ResponseEntity("200","成功上传",name);
	}


}
